﻿jQuery(function () {

    //getDaySellMinMax();
});

var isNewNonHR = undefined;
var editIndex = undefined;
function getDaySellMinMax() {
    jQuery('#dgDaySellMinMax').datagrid('reload');


    //{
    //url: "/api/DaySellMinMax/PostGetAllDaySellMinMax",
    //onDblClickCell: function (index, field1) {
    //    if (editIndex != undefined) {
    //        //jQuery('#orderDetailsList').datagrid('select', editIndex);
    //        var t = jQuery('#dgDaySellMinMax'); //My treegrid selector
    //        t.datagrid('endEdit', editIndex);
    //        editIndex = undefined;
    //        return;
    //    }

    //    editIndex = index;

    //    //jQuery(this).datagrid('beginEdit', index, field1);
    //    jQuery(this).datagrid('selectRow', index).datagrid('editCell', { index: index, field: field1 });
    //    var ed = jQuery(this).datagrid('getEditor',
    //                             { index: index, field: field1 });
    //    if (ed == null) {
    //        return;
    //    }
    //    jQuery(ed.target).focus().select().bind('keyup', function (e) {
    //        var code = e.keyCode || e.which;
    //        if (code == 13) { //Enter keycode
    //            //Trigger code to save row
    //            //This executes onAfterEdit event code
    //            var t = jQuery('#dgDaySellMinMax'); //My treegrid selector
    //            t.datagrid('endEdit', editIndex, field1);
    //            editIndex = undefined; //editIndex is a global var
    //        }
    //    });
    //}
    //});
}

function formatBoolean(val, rec) {
    if (val) {
        return '<span class="icon-ok"></span>';
    } else {
        return '<span class="icon-no"></span>';
    }
}

jQuery.extend(jQuery.fn.datagrid.methods, {
    editCell: function (jq, param) {
        return jq.each(function () {
            var opts = jQuery(this).datagrid('options');
            var fields = jQuery(this).datagrid('getColumnFields', true).concat(jQuery(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    col.editor = null;
                }
            }
            jQuery(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = jQuery(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    }
});

function deleteMinMax() {
    var minMaxSelect = jQuery('#dgDaySellMinMax').datagrid('getSelected');
    if (minMaxSelect == undefined) {
        alert("กรุณาเลือกรายการที่ต้องการลบ");
    }
    jQuery.messager.confirm('ต้องการจะลบ product id ' + minMaxSelect.product_id + '?', 'ลบข้อมูล MinMax', function (r) {
        if (r) {
            jQuery.ajax({
                type: "POST",
                url: "/api/DaySellMinMax/PostDeleteMinMax",
                data: JSON.stringify(minMaxSelect),
                contentType: "application/json",
                success: function (result) {
                    if (result == true) {
                        alert('Delete Success');
                    }
                    getDaySellMinMax();
                    clearAllInput();
                    jQuery('#dlgNonHr').dialog('close');
                }
            });
        }
    });

}

function saveMinMax() {
    var newMinMaxValue = getAllInput();
    if (validateMinMax(newMinMaxValue)) {
        if (newMinMaxValue.isEnable == "on") {
            newMinMaxValue.isEnable = true;
        }

        if (isNewNonHR == undefined || isNewNonHR == true) {
            newMinMaxValue["isInsert"] = "true";
        }
        else {
            newMinMaxValue["isInsert"] = "false";
        }

        jQuery.ajax({
            type: "POST",
            url: "/api/DaySellMinMax/PostNewMinMax",
            data: JSON.stringify(newMinMaxValue),
            contentType: "application/json",
            success: function (result) {
                if (result == true) {
                    alert('Save Success');
                }
                getDaySellMinMax();
                clearAllInput();
                jQuery('#dlgNonHr').dialog('close');
            }
        });
    }
}


function validateMinMax(data) {
    if (data.product_id == null || data.product_id == undefined || data.product_id == "") {
        alert("กรุณาระบุ รหัสสินค้า 18 หลัก");
        return false;
    }
    else if (data.location_id == null || data.location_id == undefined || data.location_id == "") {
        alert("กรุณาระบุ รหัส Branch");
        return false;
    }
    else if (data.min == null || data.min == undefined || data.min == "") {
        alert("กรุณาระบุ Min");
        return false;
    }
    else if (!jQuery.isNumeric(data.min)) {
        alert("กรุณาระบุ Min เป็นตัวเลข");
        return false;
    }
    else if (data.max == null || data.max == undefined || data.max == "") {
        alert("กรุณาระบุ Max");
        return false;
    }
    else if (!jQuery.isNumeric(data.max)) {
        alert("กรุณาระบุ Max เป็นตัวเลข");
        return false;
    }
    else if (data.rop == null || data.rop == undefined || data.rop == "") {
        alert("กรุณาระบุ Rop");
        return false;
    }
    else if (!jQuery.isNumeric(data.rop)) {
        alert("กรุณาระบุ Rop เป็นตัวเลข");
        return false;
    }

    return true;
}

function loadEdit() {
    isNewNonHR = false;
    var minMaxSelect = jQuery('#dgDaySellMinMax').datagrid('getSelected');
    if (minMaxSelect == undefined) {
        alert("กรุณาเลือกรายการที่ต้องการแก้ไข");
    }
    jQuery('#FormNonHr').form('load', minMaxSelect);
    jQuery('#isEnable').prop('checked', minMaxSelect.isEnable);

    jQuery('#dlgNonHr').dialog('open').dialog('setTitle', 'แก้ไข MinMax');
}

function addNonHR() {
    isNewNonHR = true;
    jQuery('#openSearchPopup').show();
    clearAllInput();
    jQuery('#dlgNonHr').dialog('open').dialog('setTitle', 'เพิ่ม MinMax');
}

function openSearchProduct() {
    jQuery('#productSearch').dialog('open').dialog('setTitle', 'ค้นหาสินค้า');
}

function openSearchBranch() {
    jQuery('#branchSearch').dialog('open').dialog('setTitle', 'ค้นหา Branch');
}

function searchProduct() {
    var productMatno = jQuery('#searchProductMatNo').val();
    var productThaiName = jQuery('#searchProductNameThai').val();
    var productEngName = jQuery('#searchProductNameEng').val();

    if (productMatno != '' || productThaiName != '' || productEngName != '') {
        var Query = "matno=" + productMatno + "&namethai=" + productThaiName + "&nameeng=" + productEngName;
        jQuery('#dlgProductSearch').datagrid({
            url: "/api/MaterialApi/PostSearchMaterial?" + Query,
            onLoadSuccess: function () {
                var searchRows = jQuery(this).datagrid('getRows');
                if (searchRows == 0) {
                    alert("ไม่พบรายละเอียดตามที่กำหนด");
                }
            },
            onLoadError: function () {

                alert("ไม่พบรายละเอียดตามที่กำหนด");
            }
        });

    }
    else {
        alert('กรุณาเลือกเงื่อนไขในการค้นหาสินค้า');
    }
}

function searchBranch() {
    var BranchNo = jQuery('#searchBranchNo').val();
    var BranchName = jQuery('#searchBranchName').val();

    if (BranchNo != '' || BranchName != '') {
        var Query = "branchno=" + BranchNo + "&branchname=" + BranchName;
        jQuery('#dlgBranchSearch').datagrid({
            url: "/api/BranchApi/PostBranchByNameAndId?" + Query,
            onLoadSuccess: function () {
                var searchRows = jQuery(this).datagrid('getRows');
                if (searchRows == 0) {
                    alert("ไม่พบรายละเอียดตามที่กำหนด");
                }
            },
            onLoadError: function () {

                alert("ไม่พบรายละเอียดตามที่กำหนด");
            }
        });
    }
    else {
        alert('กรุณาเลือกเงื่อนไขในการค้นหาสินค้า');
    }
}

function selectProduct() {

    var productSelect = jQuery("#dlgProductSearch").datagrid("getSelected");
    jQuery("#product_id").val(productSelect.mmatno);
    jQuery('#productSearch').dialog('close')
}

function selectBranch() {

    var branchSelect = jQuery("#dlgBranchSearch").datagrid("getSelected");
    jQuery("#location_id").val(branchSelect.mbranchno);
    jQuery("#branchName").val(branchSelect.mprovdesc);
    jQuery('#branchSearch').dialog('close')
}


function getAllInput() {
    var data = {};
    jQuery("form#FormNonHr :input").each(function () {
        var input = jQuery(this); // This is the jquery object of the input, do what you will
        var name = jQuery(input).attr("name");
        var type = jQuery(input).attr("type");

        if (type == 'checkbox') {
            var value;
            value = input.prop('checked');
            data[name] = value;
        }
        else if (name != undefined) {
            var value;
            value = jQuery.trim(input.val());
            data[name] = value;
        }
    });
    return data;
}

function clearAllInput() {
    jQuery("form#FormNonHr :input").each(function () {
        var input = jQuery(this); // This is the jquery object of the input, do what you will
        input.val('');
        return true;
    });
}

function exportDaySellMinMax() {
    window.open("/api/DaySellMinMax/GetExportAllDaySellMinMax", "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");

}
